var datas = [];
var table;
function init() {
	$('#start').val($('#startTime').val());
	$('#end').val($('#endTime').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
	loadData();
}
function loadData() {
	var s = $('#start').val();
	var e = $('#end').val();
	$.getJSON("/rest/specialReport/getSpecialReport/" + s + "/" + e, function(result) {
		datas = result;
		datas.sort(sortDatas);
		$('#tbnum').html(localeTitle.GongJi + "<span>  </span>"+datas.length +"<span>  </span>"+ localeTitle.Tiao);
		loadHtml();
	});
	function sortDatas(obj1, obj2) {
		var t1 = obj1.day.time ? obj1.day.time : obj1.day;
		var t2 = obj2.day.time ? obj2.day.time : obj2.day;
		t1 = t1 ? t1 : 0;
		t2 = t2 ? t2 : 0;
		return t2 - t1;
	}
}
function query() {
	loadData();
}
function loadHtml() {
	if (table) {
		table.getTable().destroy();
	}
	var columns = [ localeTitle.XuHao, localeTitle.ZhuanXiangLeiXing, localeTitle.ZhuanXiangNeiRong,
			localeTitle.FuJian, localeTitle.RiQi ];
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.scrollY = scrollYHeight;
	opt.columnDefs = [ {
		targets : 0,
		width : '5%'
	}, {
		targets : 1,
		width : '10%'
	}, {
		targets : 2,
		width : '45%'
	}, {
		targets : 3,
		width : '15%'
	}, {
		targets : 4,
		width : '10%'
	} ];
	table = new MyTable("reportTable", columns, opt);
	table.setDataSet(getDataset()).show();
}
function getDataset() {
	var dataset = [];
	for ( var i in datas) {
		var d = datas[i];
		var ass = '';
		if (d.fileNames.length + d.urlNames.length == 0) {
			ass = "<span style='color:darkgray;'>"+localeTitle.WuFuJian+"</span>";
		} else {
			ass += '<ul>';
			for ( var j in d.fileNames) {
				var f = d.fileNames[j];
				ass += '<li><a href="javascript:void(0)" onclick="downloadFunc(\''
						+ f.id
						+ '\');return false;"><span class="fa fa-arrow-down" style="margin-right:10px;"></span><span style="color:black;">'
						+ f.description + '</span></a>';
				ass += '</li>';
			}
			for ( var j in d.urlNames) {
				var f = d.urlNames[j];
				ass += '<li><a href="' + f.url + '" class="fa fa-arrow-down" style="margin-right:10px;" download="'
						+ f.name + '"><span style="color:black;">' + f.name + '</span></a>';
				ass += '</li>';
			}
			ass += '</ul>';
		}
		dataset.push([ Number(i) + 1, str(d.specialType), str(d.specialContect), ass, time(d.day) ]);
	}
	return dataset;
}
function downloadFunc(fileId) {
	//得到后台的file
	downloadFile([ {
		name : 'fileId',
		value : fileId
	} ]);
}
function download(){
	var file = document.getElementById("downloadForm:file");
	if(file.innerHTML == 'true'){
		document.getElementById("downloadForm:downloadBtn").click();
	}else{
		layer.msg('找不到文件！');
	}
}
function time(d) {
	if (!d) {
		return '';
	}
	var t = d.time ? d.time : d;
	if (t) {
		var date = new Date(t);
		return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
	} else {
		return '';
	}
}
function str(d) {
	if (d) {
		d = d.replace(/\r\n/g, "<br>")
		d = d.replace(/\n/g, "<br>");
		return d;
	} else {
		return '';
	}
}

function resizeHtml() {
	if (table) {
		table.getTable().draw();
	}
}
